
#include <BFont>
#include <BPainter>
#include <BMouseEvent>
#include <BKeyEvent>
#include <BScrollBar>
#include <BStyle>

#include "member_BObject.h"
#include "member_BComboBox.h"
#include "member_BComboItem.h"
#include "IAlignment.h"

using namespace BWE;

member_BComboBox::member_BComboBox(BComboBox* comboBox)
{
	boss = comboBox;
	align = Align_LeftCenter;
	form = Form_None;
	spacing = 3;
	state = State_Normal;
	arrowState = State_Normal;
	current = 0;
	readOnly = true;
	blinkTime = 1000;
	blinkTick = 0;
	inputShown = false;

	dirty = true;
	editing = false;
	pressed = false;
	hoverIndex = 0;

	board = new IComboBoard(&items, boss);
	board->setName("board");
	arrow = board->arrow;
	boss->addChild(arrow);
}
member_BComboBox::~member_BComboBox()
{

}

void member_BComboBox::freshRects()
{
	BRect crect = boss->centerRect();
	const BFont* font = this->font();
	if (items.empty())
		board->arrow->setEnabled(false);
	else
		board->arrow->setEnabled(true);
	if (arrow)
	{
		if (font && arrow->iconSize().empty())
			arrow->setIconSize(font->size() / 2);
		int arrowWidth = arrow->sizeHint().width();
		if (boss->perch().right() < arrowWidth)
			boss->setPerch(Part_Right, arrowWidth);
		BRect rightRect = boss->rect(Part_Right);
		arrow->setSize(rightRect.size());
		BRect arrowRect = rightRect.align(Align_Center, arrow->size());
		arrow->setRect(arrowRect);
	}

	Form realForm = form;
	if (form == Form_None)
	{
		for (int i = 0; i < items.size(); i++)
		{
			BComboItem* item = items[i];
			if (item_member(item)->icon || item_member(item)->image || item_member(item)->graph)
			{
				realForm = Form_IconText;
				break;
			}
		}
	}

	realIconSize.reset();
	if (realForm != Form_None && realForm != Form_TextOnly)
	{
		realIconSize = iconSize;
		if (font)
		{
			if (realIconSize.width() < 1)
				realIconSize.width() = font->size();
			if (realIconSize.height() < 1)
				realIconSize.height() = font->size();
		}
	}

	BSize textSize;
	const BStyle* style = boss->realStyle();
	for (int i = 0; i < items.size(); i++)
	{
		BComboItem* item = items[i];
		if (item_member(item)->dirty)
		{
			if (font)
			{
				if (style && readOnly)
					style->translate(item_member(item)->text, item_member(item)->t_text);
				else
					item_member(item)->t_text.clear();
				if (item_member(item)->t_text.size())
					item_member(item)->textSize = font->textSize(item_member(item)->t_text);
				else
					item_member(item)->textSize = font->textSize(item_member(item)->text);
			}
			if (current == item)
			{
				if (item_member(item)->t_text.size())
					reset(item_member(item)->t_text);
				else
					reset(item_member(item)->text);
			}
			item_member(item)->dirty = false;
		}
		if (textSize.width() < item_member(item)->textSize.width())
			textSize.width() = item_member(item)->textSize.width();
		if (textSize.height() < item_member(item)->textSize.height())
			textSize.height() = item_member(item)->textSize.height();
	}

	contentSize.reset();
	IAlignment alignment;
	alignment.ia_align = align;
	alignment.ia_form = realForm;
	alignment.ia_spacing = spacing;
	alignment.ia_imageSize = realIconSize;
	alignment.ia_textSize = textSize;
	alignment.adjust(crect);
	iconRect = alignment.ia_imageRect;
	contentSize = alignment.ia_hintSize;

	rect = alignment.ia_textRect;
	if(rect.valid())
	{
		dirtyCells = true;
		adjust(0);
	}
}
